﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder
{
    public partial class DoctorReg : Form
    {
        User user;
        MedBaseEntities med = new MedBaseEntities();
        public static ObjectSet<Profile> profiles;
        public static ObjectSet<Region> regions;
        public DoctorReg(User user)
        {
            InitializeComponent();
            profiles = med.Profiles;
            regions = med.Regions;
            this.user = user;
        }

        private void DoctorReg_Load(object sender, EventArgs e)
        {
            foreach (var x in profiles)
            {
                comboBox1.Items.Add(x.ProfName);
            }

            foreach (var x in regions)
            {
                comboBox2.Items.Add(x.Name);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckProfile())
            {
                string profileName = comboBox1.SelectedItem.ToString();
                Doctor doctor = new Doctor();
                doctor.ProfId = profiles.Where(x => x.ProfName.Equals(profileName)).First().ProfId;
                if (!comboBox2.Text.Equals(String.Empty))
                {
                    string regionName = comboBox2.SelectedItem.ToString();
                    doctor.Region = regions.Where(x => x.Name.Equals(regionName)).First().RegId;
                }

                med.AddToUsers(user);
                med.SaveChanges();
                doctor.Passport = user.Passport;
                med.AddToDoctors(doctor);
                med.SaveChanges();
                
                SuccessReg fm1 = new SuccessReg(user);
                fm1.ShowDialog();
                this.Close();
            }
        }

        private bool CheckProfile()
        {
            if (comboBox1.Text.Equals(String.Empty))
            {
                Info.Text = "Указание профиля обязательно при регистрации";
                return button1.Enabled = false;
            }
            else
            {
                Info.Text = string.Empty;
                return button1.Enabled = true;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckProfile();
        }
    }
}
